package com.css.fxfzfxqh.modules.taskMaps.repository.impl;

import com.alibaba.druid.sql.builder.SQLBuilder;
import com.alibaba.fastjson.JSON;
import com.css.fxfzfxqh.modules.comprehensiveSearch.param.QueryParam;
import com.css.fxfzfxqh.modules.taskMaps.entity.TaskMaps;
import com.css.fxfzfxqh.modules.taskMaps.repository.TaskMapsRepository;
import com.css.fxfzfxqh.modules.taskMaps.vo.MapsVO;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @Description: fxfzfxqh
 * @Author: lhl
 * @CreateDate: 2023/11/24 10:46
 */
@Repository
public class TaskMapsRepositoryImpl implements TaskMapsRepository {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public List<TaskMaps> getByParam(TaskMaps entity) {
        StringBuilder sql=new StringBuilder("select * from fxqh_task_maps");
        StringBuilder whereSql=new StringBuilder(" where del_flag='0'");
        selectSQLBuilder(entity,whereSql);
        List<TaskMaps> query = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(TaskMaps.class));
        return query;
    }

    @Override
    public int save(TaskMaps entity) {
         String sql="insert into fxqh_task_maps(id,task_id,attach_id,format,title,unit,data_type,assess_type,assess_obj," +
                 "destroy,casualties,time,seismic_input_mode,condition,del_flag,create_user,create_time,qh_type)" +
                " values(:id,:taskId,:attachId,:format,:title,:unit,:dataType,:assessType,:assessObj," +
                 ":destroy,:casualties,:times,:seismicInputMode,:condition,:delFlag,:createUser,:createTime,:qhType)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
         return namedParameterJdbcTemplate.update(sql,source);
    }

    @Override
    public int deleteById(String id, String userId, Timestamp updateTime) {
        return jdbcTemplate.update("update fxqh_task_maps set del_flag='1',update_user='"+userId+"',update_time='"+updateTime+
                "' where del_flag='0' and attach_id='"+id+"'");
    }

    @Override
    public List<MapsVO> getMapsByParam(QueryParam param) {
        StringBuilder sql=new StringBuilder("select t1.assess_type,t1.assess_obj,t1.destroy,t1.time,t1.seismic_input_mode," +
                "t1.condition,t2.attach_id,t2.attach_name,t2.attach_path,t2.file_dir_id from (select * from fxqh_task_maps");
        StringBuilder whereSql=new StringBuilder(" where del_flag='0'");
        if (PlatformObjectUtils.isNotEmpty(param.getId())){
            whereSql.append(" and task_id='").append(param.getId()).append("'");
        }

        if (PlatformObjectUtils.isNotEmpty(param.getAssessAndQh())){
            whereSql.append(" and data_type='").append(param.getAssessAndQh()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getQhType())){
            whereSql.append(" and qh_type='").append(param.getQhType()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getAssessType())){
            whereSql.append(" and assess_type='").append(param.getAssessType()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getAssessObj())){
            whereSql.append(" and assess_obj='").append(param.getAssessObj()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getDamageDegree())){
            whereSql.append(" and destroy='").append(param.getDamageDegree()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getCasualties())){
            whereSql.append(" and casualties='").append(param.getCasualties()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getTime())){
            whereSql.append(" and time='").append(param.getTime()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getSeismicInputMode())){
            whereSql.append(" and seismic_input_mode='").append(param.getSeismicInputMode()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getCondition())){
            whereSql.append(" and condition='").append(param.getCondition()).append("'");
        }
        sql.append(whereSql).append(") t1,sys_attachment_info t2 where t2.is_valid='1' and t2.is_completed='1' " +
                " and t1.attach_id=t2.attach_id ");

        List<MapsVO> query = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(MapsVO.class));

        return query;
    }

    @Override
    public List<MapsVO> getMapsByParam(String taskId, String type) {
        StringBuilder sql=new StringBuilder("select t1.assess_type,t1.assess_obj,t1.destroy,t1.time,t1.seismic_input_mode," +
                "t1.condition,t2.attach_id,t2.attach_name,t2.attach_path,t2.file_dir_id from (select * from fxqh_task_maps");
        StringBuilder whereSql=new StringBuilder(" where del_flag='0'");
        if (PlatformObjectUtils.isNotEmpty(taskId)){
            whereSql.append(" and task_id='").append(taskId).append("'");
        }
        if ("2".equals(type)){
            whereSql.append(" and data_type='2'");
        }else {
            whereSql.append(" and (data_type='0' or data_type='1')");
        }
        sql.append(whereSql).append(") t1,sys_attachment_info t2 where t2.is_valid='1' and t2.is_completed='1' " +
                " and t1.attach_id=t2.attach_id ");
        List<MapsVO> query = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(MapsVO.class));

        return query;
    }


    private void selectSQLBuilder(TaskMaps entity, StringBuilder whereSql){
        if (PlatformObjectUtils.isNotEmpty(entity.getId())){
            whereSql.append(" and id='").append(entity.getId()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getTaskId())){
            whereSql.append(" and task_id='").append(entity.getTaskId()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getAttachId())){
            whereSql.append(" and attach_id='").append(entity.getAttachId()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getFormat())){
            whereSql.append(" and format='").append(entity.getFormat()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getTitle())){
            whereSql.append(" and title='").append(entity.getTitle()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getUnit())){
            whereSql.append(" and unit='").append(entity.getUnit()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getDataType())){
            whereSql.append(" and data_type='").append(entity.getDataType()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getQhType())){
            whereSql.append(" and qh_type='").append(entity.getQhType()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getAssessType())){
            whereSql.append(" and assess_type='").append(entity.getAssessType()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getAssessObj())){
            whereSql.append(" and assess_obj='").append(entity.getAssessObj()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getDestroy())){
            whereSql.append(" and destroy='").append(entity.getDestroy()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getCasualties())){
            whereSql.append(" and casualties='").append(entity.getCasualties()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getTimes())){
            whereSql.append(" and time='").append(entity.getTimes()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getSeismicInputMode())){
            whereSql.append(" and seismic_input_mode='").append(entity.getSeismicInputMode()).append("'");
        }
        if (PlatformObjectUtils.isNotEmpty(entity.getCondition())){
            whereSql.append(" and condition='").append(entity.getCondition()).append("'");
        }
    }
}
